using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Common.NotifyEvent;
using Controls;
using Define;
using MainView.StandardCurve;
using SExperiment;
using SProject;
using SWellPlate;

namespace MainView.MainControls
{
    public partial class UCSubsetStandardCurve : UserControl, IDisposable, IComponentConnector
    {
        public static readonly DependencyProperty HighLightProperty = DependencyProperty.Register("HighLight", typeof(bool), typeof(UCSubsetStandardCurve), new UIPropertyMetadata(true));

        public static readonly DependencyProperty SubsetProperty = DependencyProperty.Register("Subset", typeof(Subset), typeof(UCSubsetStandardCurve), new UIPropertyMetadata(null, OnSubsetChanged));

        private List<UCStandardCurveChart> _StandardCurves = new List<UCStandardCurveChart>();

        private List<WellCell> HighLightWells = new List<WellCell>();

        public bool HighLight
        {
            get
            {
                return (bool)GetValue(HighLightProperty);
            }
            set
            {
                SetValue(HighLightProperty, value);
            }
        }

        public Subset Subset
        {
            get
            {
                return (Subset)GetValue(SubsetProperty);
            }
            set
            {
                SetValue(SubsetProperty, value);
            }
        }

        private static void OnSubsetChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            UCSubsetStandardCurve uCSubsetStandardCurve = sender as UCSubsetStandardCurve;
            if (uCSubsetStandardCurve != null)
            {
                uCSubsetStandardCurve.RefreshStdCurve();
            }
        }

        public UCSubsetStandardCurve()
        {
            InitializeComponent();
            AddHandler(UCStandardCurveChart.OnHighLightChangedRoutedEvent, new RoutedEventHandler(OnHighLightChanged_RoutedEvent), true);
        }

        public void Dispose()
        {
            foreach (UCStandardCurveChart standardCurf in _StandardCurves)
            {
                standardCurf.Dispose();
            }
        }

        public void RefreshFirstStdCurve(Project prj)
        {
            if (prj == null)
            {
                return;
            }
            foreach (UIElement child in grid.Children)
            {
                if (!(child is TabControl))
                {
                    continue;
                }
                foreach (TabItem item in (IEnumerable)((TabControl)child).Items)
                {
                    if (item.Header.ToString() == prj.BasicInfo.Name)
                    {
                        item.IsSelected = true;
                        break;
                    }
                }
            }
        }

        public string GetAllStandardData()
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (UCStandardCurveChart standardCurf in _StandardCurves)
            {
                string text = standardCurf.GetStatandCurveData().Replace("\r\n", "\n");
                string[] array = text.Split('\n');
                if (array.Count() > 0)
                {
                    stringBuilder.AppendLine(Utility.GetMessage("Prj_PrjName") + "," + standardCurf.Tag);
                    stringBuilder.AppendLine(Utility.GetMessage("Target") + "," + ((standardCurf.TargetStdCurve == null) ? string.Empty : standardCurf.TargetStdCurve.TargetName));
                    string[] array2 = array;
                    foreach (string value in array2)
                    {
                        stringBuilder.AppendLine(value);
                    }
                }
            }
            return stringBuilder.ToString();
        }

        public void RefreshStdCurve()
        {
            grid.Children.Clear();
            _StandardCurves.Clear();
            if (Subset == null)
            {
                return;
            }
            TabControlEx tabControlEx = null;
            List<Project> list = (from s in Subset.GetAllProject()
                                  where Subset.GetSubsetParamter(s).CurrentProjectStdCurve.TargetStdCurveList != null
                                  select s).ToList();
            foreach (Project item in list)
            {
                List<TargetStdCurve> targetStdCurveList = Subset.GetSubsetParamter(item).CurrentProjectStdCurve.TargetStdCurveList;
                UIElement uIElement = null;
                TabControlEx tabControlEx2 = null;
                foreach (TargetStdCurve item2 in targetStdCurveList)
                {
                    UCStandardCurveChart uCStandardCurveChart = new UCStandardCurveChart();
                    uCStandardCurveChart.TargetStdCurve = item2;
                    uCStandardCurveChart.Tag = item.BasicInfo.Name;
                    UCStandardCurveChart uCStandardCurveChart2 = uCStandardCurveChart;
                    uCStandardCurveChart2.SetBinding(UCStandardCurveChart.HighLightProperty, new Binding("HighLight")
                    {
                        Source = this,
                        Mode = BindingMode.TwoWay
                    });
                    _StandardCurves.Add(uCStandardCurveChart2);
                    if (targetStdCurveList.Count == 1)
                    {
                        uIElement = uCStandardCurveChart2;
                        continue;
                    }
                    if (tabControlEx2 == null)
                    {
                        tabControlEx2 = new TabControlEx();
                        uIElement = tabControlEx2;
                    }
                    TabItem tabItem = new TabItem();
                    tabItem.Style = Utility.GetResource("TabItemStylesmall") as Style;
                    tabItem.Header = item2.TargetName;
                    tabItem.Content = uCStandardCurveChart2;
                    tabControlEx2.Items.Add(tabItem);
                }
                if (uIElement == null)
                {
                    continue;
                }
                if (tabControlEx2 != null)
                {
                    tabControlEx2.SelectedIndex = 0;
                }
                if (list.Count == 1)
                {
                    grid.Children.Add(uIElement);
                    continue;
                }
                if (tabControlEx == null)
                {
                    tabControlEx = new TabControlEx();
                    grid.Children.Add(tabControlEx);
                }
                TabItem tabItem2 = new TabItem();
                tabItem2.Style = Utility.GetResource("TabItemStylesmall") as Style;
                tabItem2.Header = item.BasicInfo.Name;
                tabItem2.Content = uIElement;
                tabControlEx.Items.Add(tabItem2);
            }
            if (tabControlEx != null)
            {
                tabControlEx.SelectedIndex = 0;
            }
        }

        private void OnHighLightChanged_RoutedEvent(object sender, RoutedEventArgs e)
        {
            UCStandardCurveChart uCStandardCurveChart = e.OriginalSource as UCStandardCurveChart;
            if (uCStandardCurveChart == null && _StandardCurves.Count == 0)
            {
                return;
            }
            foreach (WellCell highLightWell in HighLightWells)
            {
                highLightWell.HighLight = uCStandardCurveChart.HighLightList != null && uCStandardCurveChart.HighLightList.Contains(highLightWell.Well.ID);
                foreach (ChartData chartDatum in highLightWell.ChartData)
                {
                    chartDatum.HighLight = highLightWell.HighLight;
                }
            }
        }

        private void wellCell_Event(object sender, EventArgs e)
        {
            PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
            if (propertyChangedEventArgsEx == null || _StandardCurves.Count == 0 || !HighLight || !(propertyChangedEventArgsEx.PropertyName == "HighLight"))
            {
                return;
            }
            List<int> list = new List<int>();
            list.AddRange((from s in HighLightWells
                           where s.HighLight
                           select s.Well.ID).ToList());
            foreach (UCStandardCurveChart standardCurf in _StandardCurves)
            {
                standardCurf.RefreshHighLightChanged(list);
            }
        }

        internal void DisplayStandartPoint(bool visible, List<WellCell> selectedWells)
        {
            if (Subset == null || _StandardCurves.Count == 0)
            {
                return;
            }
            foreach (WellCell highLightWell in HighLightWells)
            {
                highLightWell.Event -= wellCell_Event;
            }
            HighLightWells = selectedWells;
            DisplayStandardPointBySampleType(visible, SampleType.Unknown);
            DisplayStandardPointBySampleType(visible, SampleType.HighPositive);
            DisplayStandardPointBySampleType(visible, SampleType.LowPositive);
            DisplayStandardPointBySampleType(visible, SampleType.Negative);
            DisplayStandardPointBySampleType(visible, SampleType.NTC);
            DisplayStandardPointBySampleType(visible, SampleType.QC);
            DisplayStandardPointBySampleType(visible, SampleType.RetestSample);
            foreach (WellCell highLightWell2 in HighLightWells)
            {
                highLightWell2.Event += wellCell_Event;
            }
        }

        private void DisplayStandardPointBySampleType(bool visible, SampleType sampleType)
        {
            foreach (UCStandardCurveChart standartCurve in _StandardCurves)
            {
                if (standartCurve.Tag == null)
                {
                    continue;
                }
                List<Dot> list = new List<Dot>();
                List<int> list2 = new List<int>();
                if (visible && HighLightWells != null)
                {
                    List<WellCell> list3 = HighLightWells.Where((WellCell s) => s.Well.Project != null && s.Well.Project.BasicInfo.Name == standartCurve.Tag.ToString()).ToList();
                    foreach (WellCell item in list3)
                    {
                        SampleTargetItem sampleTargetItem = item.Well.Targets.Where((SampleTargetItem s) => s.SampleType == sampleType && s.TargetName == standartCurve.TargetStdCurve.TargetName).FirstOrDefault();
                        if (sampleTargetItem != null && ConstValue.IsExistCross(sampleTargetItem.Result.CnValue) && ConstValue.IsExistCross(sampleTargetItem.Result.CtValue))
                        {
                            list.Add(new Dot
                            {
                                X = Math.Log10(sampleTargetItem.Result.CnValue),
                                Y = sampleTargetItem.Result.CtValue
                            });
                            list2.Add(item.Well.ID);
                        }
                    }
                }
                standartCurve.DisplayOtherStandardCurve(visible, new NotStandartCurveData
                {
                    Color = Utility.GetDrawingSampleColor(sampleType),
                    ListPoint = list,
                    WellID = list2
                });
            }
        }
    }
}
